package dao;

import java.util.List;

import javax.persistence.Query;

import model.Author;

import org.apache.log4j.Logger;

public class AuthorDao extends AbstractDao<Author> {

	public static Logger log =Logger.getLogger(AuthorDao.class);
	
	@SuppressWarnings("unchecked")
	public List<Author> searchAllAuthors() {
		Query query = getEm().createQuery("from Author");
		List<Author> authors;
		authors = query.getResultList();
		return authors;
	}

	public List<Author> getBookAuthors(Long bookId) {
		log.info(bookId);
		Query query = getEm().createQuery("select a from model.Book b join b.authors a where b.id = :id group by a.id");
		query.setParameter("id", bookId);
		List<Author> authors;
		authors = query.getResultList();
		return authors;
	}
	
	public List<Author> getOtherAuthors(Long bookId) {
		log.info(bookId);
		Query query = getEm().createQuery("select a from model.Author a where a.id not IN (select a2.id from model.Book b join b.authors a2 where b.id = :id group by a2.id) order by a.firstName desc");
		query.setParameter("id", bookId);
		List<Author> authors;
		authors = query.getResultList();
		return authors;
	}

}
